<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-between search-form">
        <div class="flex-grow-1">
          <el-select clearable class="w200px mr-3" v-model="queryForm.buildingId" placeholder="请选择大楼" autocomplete="off">
            <el-option v-for="item in buildingStore.bildingList" :key="item.id" :label="item.name"
              :value="item.id"></el-option>
          </el-select>
          <el-button type="primary" plain @click="onRefresh()">查询</el-button>
          <el-button type="info" @click="onReset()" link>重置</el-button>

        </div>
        <div class="d-flex">
          <el-button type="primary" @click="onAdd()">新增工单</el-button>
        </div>
      </div>
    </div>

    <div class="app_card pt-4  pb-5">
      <el-table v-loading="loading" element-loading-text="查询中..." stripe size="large" :data="dataOfTable"
        height="calc(100vh - 300px)">
        <el-table-column label="ID" prop="id" align="center">
          <template #default="scope">{{ scope.row.id }}</template>
        </el-table-column>
        <el-table-column label="标题" prop="title" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="所属大楼" prop="building" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.building }}</template>
        </el-table-column>
        <el-table-column label="所属楼层" prop="floor" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.floor }}</template>
        </el-table-column>
        <el-table-column label="所属教室" prop="room" align="center">
          <template #default="scope">{{ scope.row.room }}</template>
        </el-table-column>
        <el-table-column label="报修时间" prop="createTime" align="center" width="170" show-overflow-tooltip>
          <template #default="scope">{{ resetForm('YYYY-mm-dd HH:MM', scope.row.createTime) }}</template>
        </el-table-column>
        <el-table-column label="故障类型" prop="faultType" align="center" show-overflow-tooltip>
          <template #default="scope">
            {{ findValue(workerFaultTypeOption, scope.row.faultType) }}
          </template>
        </el-table-column>
        <el-table-column label="故障描述" prop="description" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.description }}</template>
        </el-table-column>
        <el-table-column label="处理人" prop="userId" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.userId }}</template>
        </el-table-column>
        <el-table-column label="故障状态" prop="status" align="center" show-overflow-tooltip>
          <template #default="scope">
            {{ findValue(workerStatusOption, scope.row.status) }}
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template #default="scope">
            <el-button link type="primary" @click="onEditRow(scope.row)">编辑</el-button>

          </template>
        </el-table-column>

      </el-table>
      <Pagination ref="paginationRef" @handleChange="handleChange" />

    </div>

    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
  </div>
</template>

<script setup>
/**
 * 教室管理
 */
import { ref, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import useAuthStore from '@/stores/useAuthStore';
import { workerStatusList, workerFaultTypeList } from '@/apis/model/WorkerorderModel';
import { findValue } from '@/utils/EsCommon';
import { resetForm } from '@/utils/EsDate.js';
import useBuildingStore from '@/stores/useBuildingStore';

//组件
import Pagination from '@/components/pagination/index.vue';
import EditDialog from './EditDialog.vue';

// ======================================================
//                      变量声明
// ======================================================

const loading = ref(false);
const buildingStore = useBuildingStore();

// 表格查询参数
const queryForm = ref({
  buildingId: ""
})
// 表格查询参数
const queryParam = ref({
  sort: "id",
  order: "desc",
  limit: 10,
  page: 1,
  query: "",
})

const workerStatusOption = ref(workerStatusList);
const workerFaultTypeOption = ref(workerFaultTypeList);

// 组件引用
const paginationRef = ref(null);
const editDialogRef = ref(null);

// 表格数据
const dataOfTable = ref([]);

// 表格多选数据
const multipleSelection = ref([]);

// ======================================================
//                      处理事件
// ======================================================
const onReset = () => {
  queryForm.value.buildingId = ""
  onRefresh()
}
// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  paginationRef.value?.changeTotal({ totalCount: 0 })
  loadData();
}

// 点击新增按钮
const onAdd = (row = null) => {
  editDialogRef.value.show(null, "新增工单", row);
}

// 点击行内编辑按钮
const onEditRow = (row) => {
  console.log(row)
  editDialogRef.value.show(row.id, "编辑工单", row);
}

// 点击行内删除按钮
const onDeleteRow = (row) => {
  ElMessageBox.confirm("确定要删除该记录吗？", "警告").then(() => {
    EsAjax.delete(ApiUrls.articleCategoryDelete, { params: { id: row.id } })
      .then(res => {
        if (res.data.code != 200) {
          ElMessage.warning(res.data.data + res.data.msg);
          return;
        }
        loadData();
      })
      .catch(err => { ElMessage.error("网络错误：", err.message) });
  }).catch(() => { });
  console.log(row)
}

// 处理分页尺寸
const handleChange = (data = null) => {
  queryParam.value.page = data?.page;
  queryParam.value.limit = data?.limit;
  loadData();
}
// ======================================================
//                      处理事件
// ======================================================
const loadData = () => {
  let _queryParam = queryParam.value
  _queryParam.query = ''
  if (queryForm.value.buildingId) {
    _queryParam.query = "buildingId=" + queryForm.value.buildingId;
  }


  _queryParam.offset = (_queryParam.page - 1) * _queryParam.limit || 0;
  loading.value = true
  EsAjax.post(ApiUrls.workerorderDatagrid, { ..._queryParam }).then(res => {
    if (res.data.code != 200) {
      ElMessage.warning(res.data.data + res.data.msg);
      return;
    }
    let { list, totalCount } = res.data.data
    dataOfTable.value = list;
    paginationRef.value?.changeTotal({ totalCount: totalCount })
  }).catch(err => {
    ElMessage.error(err.message);
  }).finally(() => {
    loading.value = false
  })
}

onMounted(() => {
  loadData();
})

buildingStore.getBuildList()

</script>
<style lang="less" scoped></style>
  
